package com.person.yhzs.family.controller;

import com.person.yhzs.annotation.Authorized;
import com.person.yhzs.common.ReturnData;
import com.person.yhzs.common.page.PageRequest;
import com.person.yhzs.common.page.PageResponse;
import com.person.yhzs.family.entity.Family;
import com.person.yhzs.family.entity.dto.FamilyDto;
import com.person.yhzs.family.entity.dto.QueryDto;
import com.person.yhzs.family.entity.vo.FamilyPersonTreeModelVo;
import com.person.yhzs.family.entity.vo.FamilyVo;
import com.person.yhzs.family.service.FamilyService;
import com.person.yhzs.user.entity.dto.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author luolin
 * @version 1.0.0
 * @ClassName FamilyController.java
 * @Description 家谱控制类
 * @createTime 2022年05月30日 11:51:00
 */
@Api(tags = "家谱管理")
@RestController
@RequestMapping("family")
public class FamilyController {

    /**
     * @Description //家谱服务
     * @Author luolin
     * @Date 2022/5/30 11:53
     *
     **/
    @Resource
    private FamilyService familyService;

    @ApiOperation(value = "创建家谱", notes = "创建家谱接口")
    @Authorized
    @PostMapping("addFamily")
    public ReturnData addFamily(@RequestBody Family family,@ApiIgnore @RequestAttribute String userId){
        Integer result = familyService.addFamily(family,userId);
        if(result > 0){
            return ReturnData.ok("创建成功");
        }else{
            return ReturnData.fail("创建失败");
        }
    }

    @ApiOperation(value = "编辑家谱", notes = "创建家谱接口")
    @Authorized
    @PutMapping("editFamily")
    public ReturnData editFamily(@RequestBody Family family,@ApiIgnore @RequestAttribute String userId){
        Long result = familyService.editFamily(family);
        if(result > 0){
            return ReturnData.ok("编辑成功");
        }else{
            return ReturnData.fail("编辑失败");
        }
    }

    @Authorized
    @ApiOperation(value = "查询家谱", notes = "家谱接口")
    @GetMapping("queryFamilyOne")
    public ReturnData queryFamily(Long familyId){
        FamilyDto familyDto = new FamilyDto();
        familyDto.setId(familyId.intValue());
        FamilyVo familyVo = familyService.queryFamily(familyDto);
        return ReturnData.ok(familyVo);
    }

    @Authorized
    @ApiOperation(value = "家谱列表",notes = "家谱列表接口")
    @PostMapping("queryFamilyPage")
    public ReturnData queryFamilyList(@RequestBody PageRequest<QueryDto> queryDto){

        PageResponse<FamilyVo> familyVoPageResponse = familyService.queryFamilyPage(queryDto);
        return ReturnData.ok(familyVoPageResponse);
    }

    @Authorized
    @ApiOperation(value = "家谱字辈信息",notes = "家谱字辈信息")
    @GetMapping("queryFamilyWords")
    public ReturnData queryFamilyWords(Long familyId){
        List<Map<String, String>> maps = familyService.queryFamilyWords(familyId);
        return ReturnData.ok(maps);
    }

    @Authorized
    @ApiOperation(value = "查询家谱中的某个关系", notes = "查询家谱中的某个关系")
    @GetMapping("queryFamilyPersonOne")
    public ReturnData queryFamilyPersonOne(Long relationId){
        return ReturnData.ok(familyService.getFamilyPersonOneByRelationId(relationId));
    }

    @Authorized
    @ApiOperation(value = "加入家族",notes = "加入家族接口")
    @PostMapping("joinFamily")
    public ReturnData joinFamily(@ApiIgnore @RequestAttribute Long userId,Long familyId){
        familyService.joinFamily(userId,familyId);
        return ReturnData.ok("加入成功");
    }

    /**
     * @Description type:添加的人员关系，1、子女，2、兄弟，3、配偶，4、父母
     * @Description targetPersonId:目标人员的id
     * @Author luolin
     * @Date 2022/8/10 15:13
     **/
    @ApiOperation(value = "家族添加成员",notes = "家族添加成员接口")
    @PostMapping("addFamilyPerson")
    /* @RequestParam("familyId") Long familyId,
                                       @RequestParam("type") Integer type,
                                       @RequestParam("targetPersonId") Long targetPersonId, */
    public ReturnData addFamilyPerson(@RequestBody UserDto userDto){
        familyService.familyAddPerson(userDto.getFamilyId(),userDto.getType(),userDto);
        System.err.println("数据："+userDto);

        return ReturnData.ok("加入成功");
    }

    /**
     * @Description 检查手机号是否存在
     * @Author luolin
     * @Date 2022/9/29 9:59
     **/
    @ApiOperation(value = "手机号检查",notes = "手机号检查接口")
    @GetMapping("checkPhone")
    public ReturnData checkPhone(String phone){
        Boolean isExists = familyService.checkPhone(phone);
        Map<String, Boolean> map = new HashMap<>();
        map.put("exists",isExists);
        return ReturnData.ok(map);
    }

    /**
     * @Description 检查身份证号是否存在
     * @Author luolin
     * @Date 2022/9/29 9:59
     **/
    @ApiOperation(value = "身份证号检查",notes = "身份证号检查接口")
    @PostMapping("checkIdCard")
    public ReturnData checkIdCard(String idCard){
        Long aLong = familyService.checkIdCard(idCard);
        if(aLong > 0){
            return ReturnData.ok("身份证号已被使用");
        }else{
            return ReturnData.ok("身份证号可以使用");
        }
    }

    /**
     * @Description familyId:家谱的id
     * @Author luolin
     * @Date 2022/8/10 15:13
     **/
    @Authorized
    @ApiOperation(value = "家族成员",notes = "获取家族成员接口")
    @PostMapping("getFamilyPerson")
    public ReturnData getFamilyPerson(Long familyId){
        List<FamilyPersonTreeModelVo> familyPersons = familyService.getFamilyPerson(familyId);
        return ReturnData.ok(familyPersons);
    }

    @Authorized
    @ApiOperation(value = "移除家族成员", notes = "移除家族成员接口")
    @DeleteMapping("removeFamilyPersonSelf/{relationId}")
    public ReturnData removeFamilyPersonSelf(@PathVariable("relationId") Long relationId){
//        familyService.removeFamilyPersonSelf(familyId,userId);

        familyService.removeFamilyPersonSelfByRelationId(relationId);
        return ReturnData.ok("移除成功！");

    }

    @Authorized
    @ApiOperation(value = "移除家族成员(包括子女)", notes = "移除家族成员(包括子女)接口")
    @DeleteMapping("removeFamilyPersonSelfAndChild/{familyId}/{relationId}")
    public ReturnData removeFamilyPersonSelfAndChild(@PathVariable("familyId") Long familyId,@PathVariable("relationId") Long relationId){
        familyService.removeFamilyPersonSelfAndChild(familyId,relationId);
        return ReturnData.ok("移除成功！");
    }

    @Authorized
    @ApiOperation(value = "移除配偶", notes = "移除配偶接口")
    @DeleteMapping("removeFamilyPersonSelfMate/{relationId}")
    public ReturnData removeFamilyPersonSelfMate(@PathVariable("relationId") Long relationId){
        familyService.removeFamilyPersonSelfMate(relationId);
        return ReturnData.ok("移除成功！");
    }

    @Authorized
    @ApiOperation(value = "删除家谱", notes = "删除家谱接口")
    @DeleteMapping("deleteFamily/{familyId}")
    public ReturnData deleteFamily(@PathVariable("familyId") Long familyId){
        familyService.deleteFamily(familyId);
        return ReturnData.ok("删除成功！");
    }
}
